import { Injectable } from '@angular/core';
import AccountModel from '../model/Account'
import { NedbService } from "app/service/nedb.service";

@Injectable()
export class AccountService {

  _account: AccountModel

  constructor(
    private nedbService: NedbService
  ) { }

  set account(account: AccountModel) {
    this._account = account
    this.nedbService.insert(account)
  }

  get account(): AccountModel {
    return this._account
  }

  async getAccount(): Promise<AccountModel> {
    return new Promise<AccountModel>((resolve, reject) => {
      let data = new AccountModel()
      this.nedbService.find(data)
      .then((data: AccountModel[]) => {
        if (data && data.length && data.length > 0) {
          resolve(data[0])
        }else{
          reject(null)
        }
      })
      .catch(error => {
        reject(error)
      })
    })
  }

}
